var Fixed = {
	margin: 0, // margin top of fixed table
	parentObj:null, // offset parent of fixedDiv
	fixedDiv: null, // table contaner
	topObj:   null, // top of table element
	btmObj:   null, // bottom of table element
	tableObj: null, // fixed table
	theadObj: null, // fixed table header row
	shimTbl: null,  // fixed table header shim table
	shimRow: null,  // fixed table header shim row
	shimCol: null,  // fixed table header shim columns
	shimColWidth: 0,// fixed table header shim columns width
	bottomFix: false, // if ture, Fixed_bottom element is fixed at bottom of window
	shimFlg: true,  // if ture, make shim to hide select tag

	// call me when select tag is not used in fixed table
	initNoShim: function(bottomFix, margin) {
		Fixed.shimFlg = false;
		Fixed.init(margin, bottomFix);
	},

	// body.onload = init
	init: function(bottomFix, margin) {
		if (margin != null) {
			Fixed.margin = margin;
		}
		if (bottomFix == true) {
			Fixed.bottomFix = true;
		}
		
		Fixed.topObj = document.getElementById("Fixed_top");
		Fixed.btmObj = document.getElementById("Fixed_bottom");

		Fixed.fixedDiv = document.getElementById("Fixed_middle");
		if (Fixed.fixedDiv == null) {
			return;
		}
		Fixed.parentObj = Fixed.fixedDiv.offsetParent;
		Fixed.parentObj.style.margin = 0;
		Fixed.parentObj.style.padding = 0;
		
		Fixed.tableObj = Fixed.fixedDiv.getElementsByTagName("table")[0];
		if (Fixed.tableObj == null) {
			return;
		}
		Fixed.tableObj.style.margin = 0;

		Fixed.theadObj = Fixed.tableObj.getElementsByTagName("thead")[0];
		if (Fixed.theadObj == null) {
			return;
		}
		var cols = Fixed.theadObj.getElementsByTagName("th");
		if (cols == null) {
			return;
		}
		
		//border width of table top
		var firstRow = Fixed.theadObj.getElementsByTagName("tr")[0];
		var firstRowTHs = firstRow.getElementsByTagName("th");
		for (var i = 0; i < firstRowTHs.length; i++) {
			firstRowTHs[i].style.borderTopWidth = 1; //border width 1
		}

		//create shim
		if (Fixed.shimFlg == true) {
			Fixed.shimRow = document.createElement("<iframe frameborder=0></iframe>");
			Fixed.shimRow.setAttribute("id", "Fixed_shimRow");
			Fixed.fixedDiv.appendChild(Fixed.shimRow);
			Fixed.fixedDiv.onscroll = function() {
				Fixed.shimRow.style.display = "none";
				Fixed.shimRow.style.display = "";
			}
			
			for (var i = 0; i < cols.length; i++) {
				var classNameTmp = " " + cols[i].className + " ";
				if (classNameTmp.search(/[ \""]+locked[ \""]+/) >= 0) {
					Fixed.shimColWidth += (cols[i].offsetWidth);
				}
			}
			if (Fixed.shimColWidth > 0) {
				Fixed.shimCol = document.createElement("<iframe frameborder=0></iframe>");
				Fixed.shimCol.setAttribute("id", "Fixed_shimCol");
				Fixed.fixedDiv.appendChild(Fixed.shimCol);
			}
			Fixed.shimTbl = document.createElement("div");
			Fixed.shimTbl.setAttribute("id", "Fixed_shimTbl");
			Fixed.fixedDiv.appendChild(Fixed.shimTbl);
		}

		window.onresize = Fixed.resize;
		Fixed.resize();
	},
	
	// call me when table resized
	resize: function() {
		if (Fixed.fixedDiv == null || Fixed.tableObj == null) {
			return;
		}

		var topOfTblHeight = 0;
		if (Fixed.topObj != null) {
			topOfTblHeight = Fixed.topObj.offsetHeight
			 + Fixed.parsePxInt(Fixed.topObj.style.marginTop)
			 + Fixed.parsePxInt(Fixed.topObj.style.marginBottom);
		}

		var btmOfTblHeight = 0;
		if (Fixed.btmObj != null) {
			btmOfTblHeight = Fixed.btmObj.offsetHeight
			 + Fixed.parsePxInt(Fixed.btmObj.style.marginTop)
			 + Fixed.parsePxInt(Fixed.btmObj.style.marginBottom);
		}

		var tblMargin = Fixed.parsePxInt(Fixed.fixedDiv.style.marginTop)
			 + Fixed.parsePxInt(Fixed.fixedDiv.style.marginBottom)
			 + Fixed.margin;
			 
		var outOfTblHeight = topOfTblHeight
			 + btmOfTblHeight
			 + tblMargin;

		var fixedDivHeight = 0;
		if (Fixed.bottomFix == false &&
			Fixed.parentObj.clientHeight - Fixed.tableObj.offsetHeight - outOfTblHeight >= 0) {
			fixedDivHeight = Fixed.tableObj.offsetHeight;
			var tmp = Fixed.parentObj.clientHeight - Fixed.tableObj.offsetHeight - outOfTblHeight
			if (Fixed.fixedDiv.clientWidth <= Fixed.tableObj.offsetWidth) {
				if (tmp <= 17) { // scroll bar size 17
					fixedDivHeight += tmp;
				} else {
					fixedDivHeight += 17;
				}
			}
		} else if (Fixed.bottomFix == true &&
			Fixed.parentObj.clientHeight - Fixed.tableObj.offsetHeight - outOfTblHeight >= 0) {
			fixedDivHeight = Fixed.parentObj.clientHeight - outOfTblHeight;
			
		} else if (Fixed.parentObj.clientHeight - Fixed.tableObj.offsetHeight - outOfTblHeight < 0) {
			fixedDivHeight = Fixed.parentObj.clientHeight - outOfTblHeight;
		} else {
			fixedDivHeight = Fixed.tableObj.offsetHeight;
		}

		if (fixedDivHeight <= 0) {
			fixedDivHeight = 1;
		}

		Fixed.fixedDiv.style.height = fixedDivHeight;

		// change shim size
		if (Fixed.shimFlg == true) {
			Fixed.shimRow.style.height = Fixed.theadObj.offsetHeight;
			Fixed.shimRow.style.width = Fixed.theadObj.offsetWidth;
	
			if (Fixed.shimCol != null) {
				var shimColHeightTmp = 0;
				if (Fixed.bottomFix == true &&
					Fixed.parentObj.clientHeight - Fixed.tableObj.offsetHeight - outOfTblHeight >= 0) {
					shimColHeightTmp = Fixed.tableObj.clientHeight;
				} else {
					shimColHeightTmp = Fixed.fixedDiv.clientHeight;
				}
				if (Fixed.tableObj.clientHeight < shimColHeightTmp) {
				}
				Fixed.shimCol.style.height = shimColHeightTmp;
				Fixed.shimCol.style.width = Fixed.shimColWidth;
				Fixed.shimTbl.style.height = shimColHeightTmp;
				Fixed.shimTbl.style.width = Fixed.shimColWidth;
			}
		}
	 },
	 parsePxInt: function(value) {
		var tmp = value.match(/^\d+/);
		if (tmp == null || isNaN(tmp)) {
			return 0;
		}
		return parseInt(tmp);
	 }
}
